package com.gusha.service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysTrainingDao;
import com.gusha.model.SysTraining;

@Service
@Transactional
public class SysTrainingService {
	@Autowired
	private SysTrainingDao sysTrainingDao;
	
	 public Page<SysTraining> findAll(Pageable pageable, SysTraining sysTraining) {
	        @SuppressWarnings("serial")
	        Page<SysTraining> result = sysTrainingDao.findAll(new Specification<SysTraining>() {
	            @Override
	            public Predicate toPredicate(Root<SysTraining> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
	                List<Predicate> list = new ArrayList<Predicate>();
	                if (sysTraining.getId() != null) {
	                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysTraining.getId()));
	                }
	                if (sysTraining.getTrainingTitle() != null) {
	                    list.add(cb.equal(root.get("trainingTitle").as(String.class),sysTraining.getTrainingTitle()));
	                }
	                if (sysTraining.getTrainingVideo() != null) {
	                    list.add(cb.equal(root.get("trainingVideo").as(String.class),sysTraining.getTrainingVideo()));
	                }
	                if (sysTraining.getUpdateTime() != null) {
	                    list.add(cb.equal(root.get("updateTime").as(Date.class),sysTraining.getUpdateTime()));
	                }
	                if (sysTraining.getUpdateName() != null) {
	                    list.add(cb.equal(root.get("updateName").as(String.class),sysTraining.getUpdateName()));
	                }
	                if (sysTraining.getAddTime() != null) {
	                    list.add(cb.equal(root.get("addTime").as(Date.class),sysTraining.getAddTime()));
	                }
	                if (sysTraining.getAddName() != null) {
	                    list.add(cb.equal(root.get("addName").as(String.class),sysTraining.getAddName()));
	                }
	                if (sysTraining.getTrainDescribe() != null) {
	                    list.add(cb.equal(root.get("trainDescribe").as(String.class),sysTraining.getTrainDescribe()));
	                }
	                if (sysTraining.getIsDelete() != null) {
	                    list.add(cb.equal(root.get("isDelete").as(BigInteger.class),sysTraining.getIsDelete()));
	                }
	                if (sysTraining.getTrainImg() != null) {
	                    list.add(cb.equal(root.get("trainImg").as(String.class),sysTraining.getTrainImg()));
	                }
	                
	                Predicate[] p = new Predicate[list.size()];
	                return cb.and(list.toArray(p));
	            }
	        },pageable);
	         return result;
	    }

}
